package com.pcbly.web.components.utils.template.dwr;

import java.io.File;
import java.lang.reflect.Field;
  
import com.pcbly.web.bean.user.UserSchoolBean;
import com.pcbly.web.components.utils.ClassUtilsStatic;
import com.pcbly.web.components.utils.template.TemplateModel;

public class ModelDwrGenerators {
	public void execute(Class<?> clz) {
		try {
			String pachageString = clz.getPackage().getName();
			String path = pachageString.replaceAll("\\.", "/");
			String pacha = pachageString;
			File file = new File("src/" + path);
			if (file.isDirectory()) {
				String[] files = file.list();
				for (String fi : files) {
					if (fi.indexOf("java") >= 0) {
						String te = fi.substring(0, fi.indexOf("."));
						te = pacha + "." + te;
						Class<?> temp = ClassUtilsStatic.forName(te);
						if (!temp.isInterface()) {
//							System.out.println(temp.getName());
							TemplateModel model = new TemplateModel();
							model.setClz(temp);
							model.setClzName(temp.getName());
							Field[] fields = temp.getDeclaredFields();
							Field[] tempFields = new Field[fields.length - 1];
							int i = 0;
							for (Field field : fields) {
								if (!(field.getName()
										.equals("serialVersionUID")
										|| field.getName().equals("endDate") || field
										.getName().equals("startDate"))) {
									tempFields[i] = field;
									i++;
								}
							}
							ModelDwr dwr = new ModelDwr();
							dwr.setModel(model);
							dwr.setPrint(print);
							dwr.setFields(tempFields);
							dwr.execute();
							dwr.printMessage();
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private boolean print;

	public boolean isPrint() {
		return print;
	}

	public void setPrint(boolean print) {
		this.print = print;
	}

	public static void main(String[] args) {
		System.out.println("Begin...................");
		ModelDwrGenerators model = new ModelDwrGenerators();
		model.setPrint(true);
		model.execute(UserSchoolBean.class);

		System.out.println("End...................");
	}

}
